/**
 * Copyright 2015 Software innovation and R & D center. All rights reserved.
 * File Name: TemplateUtils.java
 * Encoding UTF-8
 * Version: 0.0.1
 * History:	2017年1月16日
 */
package net.huashitong.ssydt.provider.gexin.util;

import com.alibaba.fastjson.JSON;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.base.payload.Payload;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import net.huashitong.ssydt.provider.push.PushMessage;

/**
 * @version Revision: 0.0.1
 * @author: huiyang.yu
 * @Date: 2017年1月16日
 */
public class TemplateUtils {


    private TemplateUtils() {
    }

    /**
     * 返回透传消息模板
     *
     * @param message
     * @return
     */
    public static TransmissionTemplate getTransmissionTemplate(PushMessage message, String appId, String appKey) {
        String json = JSON.toJSONString(message);
        TransmissionTemplate template = new TransmissionTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        template.setTransmissionContent(json);
        template.setTransmissionType(2);//收到消息是否立即启动应用，1为立即启动，2则广播等待客户端自启动
        template.setAPNInfo(getPayload(message, json));
        return template;
    }

    /**
     * iOS推送使用
     *
     * @param message
     * @param json
     * @return
     */
    private static Payload getPayload(PushMessage message, String json) {
        APNPayload payload = new APNPayload();
        //在已有数字基础上加1显示，设置为-1时，在已有数字上减1显示，设置为数字时，显示指定数字
        payload.setAutoBadge("+1");
        payload.setContentAvailable(1);
        payload.setSound("default");
        payload.setCategory("$由客户端定义");
        //简单模式APNPayload.SimpleMsg
        //payload.setAlertMsg(new APNPayload.SimpleAlertMsg("hello"));
        //字典模式使用下者
        payload.setAlertMsg(getDictionaryAlertMsg(message));
        //增加自定义的数据
        payload.addCustomMsg("data", json);
        return payload;
    }

    private static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(PushMessage message) {
        APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
        alertMsg.setBody(message.getContext());
        alertMsg.setActionLocKey("启动应用");
        alertMsg.setLocKey(message.getContext());
        alertMsg.addLocArg("loc-args");
        alertMsg.setLaunchImage("launch-image");
        // iOS8.2以上版本支持
        alertMsg.setTitle(message.getTitle());
        alertMsg.setTitleLocKey(message.getTitle());
        alertMsg.addTitleLocArg("TitleLocArg");
        return alertMsg;
    }


}
